package com.smart.community.commons.test;

import com.smart.community.commons.service.BaseServiceCaller;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * BaseServiceCaller验证类
 * 
 * 验证BaseServiceCaller的各种方法，确保安全调用功能正常工作
 * 这是一个简单的验证工具，用于测试BaseServiceCaller的功能
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-08-28
 */
@Slf4j
public class BaseServiceCallerValidation {
    
    /**
     * 验证BaseServiceCaller的所有功能
     */
    public static void validateBaseServiceCaller() {
        log.info("开始验证BaseServiceCaller功能...");
        
        TestServiceCaller testServiceCaller = new TestServiceCaller();
        
        // 验证安全调用成功的情况
        validateSafeCallSuccess(testServiceCaller);
        
        // 验证安全调用失败的情况
        validateSafeCallFailure(testServiceCaller);
        
        // 验证安全调用无返回值成功的情况
        validateSafeCallVoidSuccess(testServiceCaller);
        
        // 验证安全调用无返回值失败的情况
        validateSafeCallVoidFailure(testServiceCaller);
        
        // 验证批量安全调用成功的情况
        validateSafeBatchCallSuccess(testServiceCaller);
        
        // 验证批量安全调用部分失败的情况
        validateSafeBatchCallPartialFailure(testServiceCaller);
        
        // 验证批量安全调用空列表的情况
        validateSafeBatchCallEmptyList(testServiceCaller);
        
        // 验证批量安全调用null列表的情况
        validateSafeBatchCallNullList(testServiceCaller);
        
        log.info("BaseServiceCaller功能验证完成！");
    }
    
    /**
     * 验证安全调用成功的情况
     */
    private static void validateSafeCallSuccess(TestServiceCaller testServiceCaller) {
        log.info("验证安全调用成功的情况...");
        
        String expectedResult = "success";
        Supplier<String> supplier = () -> expectedResult;
        
        String result = testServiceCaller.testSafeCall("测试服务", "testMethod", supplier, "default");
        
        if (expectedResult.equals(result)) {
            log.info("✅ 安全调用成功验证通过");
        } else {
            log.error("❌ 安全调用成功验证失败，期望: {}, 实际: {}", expectedResult, result);
        }
    }
    
    /**
     * 验证安全调用失败的情况
     */
    private static void validateSafeCallFailure(TestServiceCaller testServiceCaller) {
        log.info("验证安全调用失败的情况...");
        
        String defaultValue = "default";
        Supplier<String> supplier = () -> {
            throw new RuntimeException("测试异常");
        };
        
        String result = testServiceCaller.testSafeCall("测试服务", "testMethod", supplier, defaultValue);
        
        if (defaultValue.equals(result)) {
            log.info("✅ 安全调用失败验证通过");
        } else {
            log.error("❌ 安全调用失败验证失败，期望: {}, 实际: {}", defaultValue, result);
        }
    }
    
    /**
     * 验证安全调用无返回值成功的情况
     */
    private static void validateSafeCallVoidSuccess(TestServiceCaller testServiceCaller) {
        log.info("验证安全调用无返回值成功的情况...");
        
        boolean[] executed = {false};
        Runnable runnable = () -> executed[0] = true;
        
        testServiceCaller.testSafeCallVoid("测试服务", "testMethod", runnable);
        
        if (executed[0]) {
            log.info("✅ 安全调用无返回值成功验证通过");
        } else {
            log.error("❌ 安全调用无返回值成功验证失败");
        }
    }
    
    /**
     * 验证安全调用无返回值失败的情况
     */
    private static void validateSafeCallVoidFailure(TestServiceCaller testServiceCaller) {
        log.info("验证安全调用无返回值失败的情况...");
        
        Runnable runnable = () -> {
            throw new RuntimeException("测试异常");
        };
        
        try {
            testServiceCaller.testSafeCallVoid("测试服务", "testMethod", runnable);
            log.info("✅ 安全调用无返回值失败验证通过");
        } catch (Exception e) {
            log.error("❌ 安全调用无返回值失败验证失败，异常: {}", e.getMessage());
        }
    }
    
    /**
     * 验证批量安全调用成功的情况
     */
    private static void validateSafeBatchCallSuccess(TestServiceCaller testServiceCaller) {
        log.info("验证批量安全调用成功的情况...");
        
        List<String> items = Arrays.asList("item1", "item2", "item3");
        Function<String, String> processor = item -> "processed_" + item;
        
        List<String> results = testServiceCaller.testSafeBatchCall("测试服务", "testMethod", items, processor);
        
        if (results.size() == 3 && 
            "processed_item1".equals(results.get(0)) &&
            "processed_item2".equals(results.get(1)) &&
            "processed_item3".equals(results.get(2))) {
            log.info("✅ 批量安全调用成功验证通过");
        } else {
            log.error("❌ 批量安全调用成功验证失败，结果: {}", results);
        }
    }
    
    /**
     * 验证批量安全调用部分失败的情况
     */
    private static void validateSafeBatchCallPartialFailure(TestServiceCaller testServiceCaller) {
        log.info("验证批量安全调用部分失败的情况...");
        
        List<String> items = Arrays.asList("item1", "item2", "item3");
        Function<String, String> processor = item -> {
            if ("item2".equals(item)) {
                throw new RuntimeException("处理item2失败");
            }
            return "processed_" + item;
        };
        
        List<String> results = testServiceCaller.testSafeBatchCall("测试服务", "testMethod", items, processor);
        
        if (results.size() == 2 && 
            "processed_item1".equals(results.get(0)) &&
            "processed_item3".equals(results.get(1))) {
            log.info("✅ 批量安全调用部分失败验证通过");
        } else {
            log.error("❌ 批量安全调用部分失败验证失败，结果: {}", results);
        }
    }
    
    /**
     * 验证批量安全调用空列表的情况
     */
    private static void validateSafeBatchCallEmptyList(TestServiceCaller testServiceCaller) {
        log.info("验证批量安全调用空列表的情况...");
        
        List<String> items = Arrays.asList();
        Function<String, String> processor = item -> "processed_" + item;
        
        List<String> results = testServiceCaller.testSafeBatchCall("测试服务", "testMethod", items, processor);
        
        if (results.isEmpty()) {
            log.info("✅ 批量安全调用空列表验证通过");
        } else {
            log.error("❌ 批量安全调用空列表验证失败，结果: {}", results);
        }
    }
    
    /**
     * 验证批量安全调用null列表的情况
     */
    private static void validateSafeBatchCallNullList(TestServiceCaller testServiceCaller) {
        log.info("验证批量安全调用null列表的情况...");
        
        List<String> items = null;
        Function<String, String> processor = item -> "processed_" + item;
        
        List<String> results = testServiceCaller.testSafeBatchCall("测试服务", "testMethod", items, processor);
        
        if (results.isEmpty()) {
            log.info("✅ 批量安全调用null列表验证通过");
        } else {
            log.error("❌ 批量安全调用null列表验证失败，结果: {}", results);
        }
    }
    
    /**
     * 测试类，继承BaseServiceCaller用于测试
     */
    private static class TestServiceCaller extends BaseServiceCaller {
        
        public <T> T testSafeCall(String serviceName, String methodName, Supplier<T> supplier, T defaultValue) {
            return safeCall(serviceName, methodName, supplier, defaultValue);
        }
        
        public void testSafeCallVoid(String serviceName, String methodName, Runnable runnable) {
            safeCall(serviceName, methodName, runnable);
        }
        
        public <T, R> List<R> testSafeBatchCall(String serviceName, String methodName, List<T> items, Function<T, R> processor) {
            return safeBatchCall(serviceName, methodName, items, processor);
        }
    }
}
